package com.shameit.goodqrinfomanager.ui.activity.sync;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.os.Bundle;

import com.chad.library.BR;
import com.shameit.goodqrinfomanager.AppViewModelFactory;
import com.shameit.goodqrinfomanager.BaseMVVMActivity;
import com.shameit.goodqrinfomanager.R;
import com.shameit.goodqrinfomanager.adapter.SyncLogAdapter;
import com.shameit.goodqrinfomanager.bean.enity.SyncLogBean;
import com.shameit.goodqrinfomanager.bean.litepal.CangKu;
import com.shameit.goodqrinfomanager.bean.litepal.CangWei;
import com.shameit.goodqrinfomanager.bean.litepal.ChanPin;
import com.shameit.goodqrinfomanager.bean.litepal.JingXiaoShang;
import com.shameit.goodqrinfomanager.databinding.ActivitySyncLogBinding;

import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.List;

public class SyncLogActivity extends BaseMVVMActivity<ActivitySyncLogBinding,SyncLogViewModel> {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setToolBarBack(binding.toolbar);
    }

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_sync_log;
    }

    @Override
    public int initVariableId() {
        return BR.viewmodel;
    }

    @Override
    public SyncLogViewModel initViewModel() {
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getApplication());
        return ViewModelProviders.of(this, factory).get(SyncLogViewModel.class);
    }

    @Override
    protected String setTitle() {
        return "日志";
    }

    @Override
    protected void initView() {
        binding.rvActions.setLayoutManager(new LinearLayoutManager(this));
        binding.rvActions.setAdapter(viewModel.adapter = new SyncLogAdapter(R.layout.item_sync_log));
    }

    @Override
    protected void loadData() {
        List<SyncLogBean> list = new ArrayList<>();
        int type = getIntent().getIntExtra("type", 0);
        switch (type) {
            case 0:
                List<CangKu> cangKuList = LitePal.order("warehouseNo").find(CangKu.class);
                for (CangKu ck : cangKuList) {
                    SyncLogBean bean = new SyncLogBean();
                    bean.setCode(ck.getWarehouseNo());
                    bean.setMsg(ck.getWarehouseName());
                    list.add(bean);
                }
                break;

            case 1:
                List<CangWei> cangWeiList = LitePal.order("placeNo").find(CangWei.class);
                for (CangWei cw : cangWeiList) {
                    SyncLogBean bean = new SyncLogBean();
                    bean.setCode(cw.getPlaceNo());
                    bean.setMsg(cw.getPlaceArea());
                    list.add(bean);
                }
                break;

            case 2:
                //产品
                List<ChanPin> chanPins = LitePal.order("productCode").find(ChanPin.class);
                for (ChanPin cp : chanPins) {
                    SyncLogBean bean = new SyncLogBean();
                    bean.setCode(cp.getProductCode());
                    bean.setMsg(cp.getProductName());
                    list.add(bean);
                }
                break;
            case 3:
                List<JingXiaoShang> jxsList = LitePal.order("dealerCode").find(JingXiaoShang.class);
                for (JingXiaoShang bean : jxsList) {
                    SyncLogBean jxsBean = new SyncLogBean();
                    jxsBean.setCode(bean.getDealerCode());
                    jxsBean.setMsg(bean.getDealerName());
                    list.add(jxsBean);
                }
                break;
        }
        viewModel.adapter.setNewInstance(list);
    }
}